package com.ydy.service.impl;

import com.github.davidfantasy.jwtshiro.JWTUserAuthService;
import com.github.davidfantasy.jwtshiro.UserInfo;
import com.ydy.service.StudentBiz;
import com.ydy.vo.Studentvo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
@RequiredArgsConstructor
public class JWTStudentAuthServiceImpl implements JWTUserAuthService {
    @Autowired
    private StudentBiz studentBiz ;
    private final StringRedisTemplate stringRedisTemplate;


    @Override
    public UserInfo getUserInfo(String s) {
        UserInfo userInfo =new UserInfo();
        Studentvo studentvoWithRedis = studentBiz.getSrudentvowithAccount(s);
//        StudentInfo studentInfo = new StudentInfo();
        if (null == studentvoWithRedis.getId()){
            return  null ;

        }
        userInfo.setAccount(studentvoWithRedis.getName());
        userInfo.setSecret(studentvoWithRedis.getPassword());
        return userInfo;

    }

    @Override
    public  void  onAuthenticationFailed(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
    }

    @Override
    public void onAuthorizationFailed(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        httpServletResponse.setStatus(HttpStatus.FORBIDDEN.value());
    }

}
